<?php
/*
** Arukereso.hu
** CSV export for PrestaShop
**
** You have to put this file into your webshops main directory, and it will be work.
**
*/

if(!file_exists('init.php') || !file_exists('config/config.inc.php')) {
  error('An include file not found. Check the path.');
}
include('config/config.inc.php');
require_once('init.php');

$base_dir = 'http://' . htmlspecialchars($_SERVER['HTTP_HOST'], ENT_COMPAT, 'UTF-8') . __PS_BASE_URI__;
$base_image_dir = 'http://' . htmlspecialchars($_SERVER['HTTP_HOST'], ENT_COMPAT, 'UTF-8') . _THEME_PROD_DIR_;

$query = Db::getInstance()->ExecuteS('SELECT p.id_product FROM ' . _DB_PREFIX_ . 'product p WHERE p.active = 1 '); // "AND quantity > 0"
if(!$query) {
  error('Mysql error: ' . mysql_error());
}

$rewriting = Configuration::get('PS_REWRITING_SETTINGS');

$data = array();
foreach ($query as $i => $v) {

  $product = new Product(intval($v['id_product']),true,intval(_USER_ID_LANG_));
  $cat = new Category($product->id_category_default,intval(_USER_ID_LANG_));

  $parents = $cat->getParentsCategories();
  $category = array();
  foreach ($parents as $p) {
    $category[] = $p['name'];
  }
  $category = implode(' > ',array_reverse($category));

  if($rewriting) {
    $url = $base_dir . $product->id . '-' . $product->link_rewrite . '.html';
  } else {
    $url = $base_dir . 'product.php?id_product=' . $product->id;
  }

  $img = $product->getCover($product->id);
  if ($img['id_image'] > 0) {
    $image = $base_image_dir . $product->id . '-' . $img['id_image'] . '-large.jpg';
  } else {
    $image = "";
  }

  $price = Product::getPriceStatic($product->id, true, NULL, 6);

  if (isset(Product::$_taxCalculationMethod) && Product::$_taxCalculationMethod == PS_TAX_INC) {
    $price = Tools::ps_round($price,2);
  } else {
    $price = round($price,2);
  }

  $line['manufacturer'] = clean($product->manufacturer_name);
  $line['name'] = clean($product->name);
  $line['category'] =  clean($category);
  $line['product_url'] =  clean($url);
  $line['price'] =  $price;
  $line['image_url'] =  clean($image);
  $line['description'] = clean($product->description,false);

  $data[] = $line;
}

csv_export($data);

function clean($field,$filter_html = true) {
  if($filter_html) {
    $pattern = array();
    $replacement = array();
    $pattern[] = "/<[^>]+>/"; $replacement[] = " ";
    $pattern[] = "/  +/"; $replacement[] = " ";
    $field = preg_replace($pattern,$replacement,$field);
  }
  $from = array('"',"\r","\n");
  $to =   array('""',""," ");
  return str_replace($from,$to,$field);
}

function csv_export($data) {
  header('Cache-Control: no-cache');
  header('Content-Type: text/csv');
  header('Content-disposition: attachment; filename=ak_export.csv');
  $header = '"manufacturer","name","category","product_url","price","image_url","description"';
  echo "$header\n";
  foreach ($data as $line) {
    echo '"';
    echo implode('","',$line);
    echo "\"\n";
  }
  exit;
}

function error($msg) {
  header('Cache-Control: no-cache');
  header('Content-Type: text/html; charset=utf-8');
  echo "<html><head><title>Export error</title></head><body>";
  echo "<p style=\"padding:0;margin:50px;text-align:center;font-weight:bold;\">$msg</p>";
  echo "</body></html>";
  exit;
}

?>